package com.nagat.ca;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.awt.Toolkit;

public class Main {

	private JFrame frame;
	private JTextField miyao;
	private JTextField mw;
	private JTextField miw;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/com/nagat/ca/2.jpg")));
		frame.setResizable(false);
		frame.setTitle("\u5C0F\u718A\u52A0\u5BC6");
		frame.setBounds(100, 100, 267, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel label = new JLabel("\u5BC6\u94A5");
		label.setBounds(28, 55, 54, 15);
		panel.add(label);
		
		miyao = new JTextField();
		miyao.setBounds(92, 52, 127, 21);
		panel.add(miyao);
		miyao.setColumns(10);
		
		JLabel label_1 = new JLabel("\u660E\u6587");
		label_1.setBounds(28, 91, 54, 15);
		panel.add(label_1);
		
		mw = new JTextField();
		mw.setBounds(92, 88, 127, 21);
		panel.add(mw);
		mw.setColumns(10);
		
		JLabel label_2 = new JLabel("\u5BC6\u6587");
		label_2.setBounds(28, 129, 54, 15);
		panel.add(label_2);
		
		miw = new JTextField();
		miw.setBounds(92, 126, 127, 21);
		panel.add(miw);
		miw.setColumns(10);
		
		JButton button = new JButton("\u52A0\u5BC6");
		button.setBounds(40, 197, 81, 25);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				System.out.println(miyao.getText());
				System.out.println(mw.getText());
				try {
					miw.setText(new BASE64Encoder().encode(mw.getText().getBytes("GBK")));
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				System.out.println(miw.getText());
				
			}
		});
		panel.add(button);
		
		JButton btnNewButton = new JButton("\u89E3\u5BC6");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					mw.setText(new String(new BASE64Decoder().decodeBuffer(miw.getText()),"GBK"));
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(149, 197, 70, 25);
		panel.add(btnNewButton);
	}
}
